package com.xiaoge.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoge.biz.entity.ProductCategory;
import com.xiaoge.biz.mapper.ProductCategoryMapper;
import com.xiaoge.biz.service.ProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaoge
 * @since 2021-03-21
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 获取所有物资分类列表
     *
     * @return
     */
    @Override
    public List<ProductCategory> getAll() {
        QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        List<ProductCategory> productCategories = (List<ProductCategory>) valueOperations.get("ProductCategory_all");
        if (CollectionUtils.isEmpty(productCategories)) {
            productCategories = productCategoryMapper.selectList(queryWrapper.orderByAsc("pid"));
            valueOperations.set("ProductCategory_all", productCategories);
        }
        return productCategories;
    }

    /**
     * 删除物资分类
     *
     * @param id
     */
    @Override
    public void delMenu(Long id) {
        productCategoryMapper.deleteById(id);
        redisTemplate.delete("ProductCategory_all");
    }

    /**
     * 修改物资分类
     *
     * @param category
     */
    @Override
    public void updateMenu(ProductCategory category) {
        productCategoryMapper.updateById(category);
        redisTemplate.delete("ProductCategory_all");
    }

    /**
     * 添加物资分类
     *
     * @param category
     */
    @Override
    public void add(ProductCategory category) {
        productCategoryMapper.insert(category);
        redisTemplate.delete("ProductCategory_all");
    }



    /**
     * 构建menuTree
     *
     * @return
     */
    @Override
    public List<ProductCategory> categoryTreeBuild(List<ProductCategory> productCategories) throws CloneNotSupportedException {
        if (productCategories.size() == 0) {
            throw new Error("未查询到菜单信息");
        }
        //主菜单
        List<ProductCategory> menuList = new ArrayList<>();
        //子菜单
        List<ProductCategory> subMenuList = new ArrayList<>();
        //分类菜单的位置，
        for (ProductCategory menu : productCategories) {
            if (menu.getPid() == 0) {
                menuList.add(menu);
            } else if (menu.getPid() != 0) {
                subMenuList.add(menu);
            }
        }


        //子菜单组合到主菜单
        for (ProductCategory menu : subMenuList) {
            findSubMenu(productCategories, menu);
        }


        //排序
        sortSubMenu(menuList);
        return menuList;
    }

    //递归将子菜单组合到父菜单的children中
    private void findSubMenu(List<ProductCategory> productCategories, ProductCategory item) throws CloneNotSupportedException {
        for (ProductCategory productCategory : productCategories) {
            if (item.getPid().equals(productCategory.getId())) {
                productCategory.add(item);
                return;
            }
        }
    }

    //递归将children中的数据排序
    private void sortSubMenu(List<ProductCategory> productCategories) {
        productCategories.sort(ProductCategory.order());
        for (ProductCategory productCategory : productCategories) {
            if (productCategory.getChildren().size() > 0) {
                sortSubMenu(productCategory.getChildren());
            }
        }
    }
}
